<template>
  <el-dialog
      v-model="scmiEditWinDialogVisible"
      title="编辑点检保养项目"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="editScmi">
        <el-form-item label="项目编码">
          <el-input v-model="editScmi.scmiEncode"/>
        </el-form-item>
        <el-form-item label="项目名称">
          <el-input v-model="editScmi.scmiName"/>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-radio-group v-model="editScmi.scmiType">
            <el-radio :value="1" size="large">设备点检</el-radio>
            <el-radio :value="2" size="large">设备保养</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="editScmi.scmiStatus">
            <el-radio :value="1" size="large">是</el-radio>
            <el-radio :value="2" size="large">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="项目内容">
          <el-input type="textarea" v-model="editScmi.scmiContent"/>
        </el-form-item>
        <el-form-item label="标准">
          <el-input type="textarea" v-model="editScmi.scmiStandard"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editScmi.scmiRemark"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editScmiSubmit">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import scmiRequest from "@/api/scmi.js";

const props = defineProps(['scmiId']);
const emits = defineEmits(['cancelEdit','editComplete' ])
let scmiEditWinDialogVisible = ref(false)
let editScmi = ref({})


function cancel() {
  emits("cancelEdit");
  scmiEditWinDialogVisible.value = false;
}

function handleClose() {
  emits("cancelEdit");
}

function loadScmiById() {
  scmiRequest.selectById(props.scmiId, res => {
    if (res.data) {
      editScmi.value = res.data;
      scmiEditWinDialogVisible.value = true;
    }
  })
}

function editScmiSubmit() {
  scmiRequest.update(editScmi.value, res => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      scmiEditWinDialogVisible.value = false;
      // 触发编辑完成的事件
      emits('editComplete');
    } else {
      ElMessage({
        type: "warning",
        message: "编辑失败"
      });
    }
  })
}

watch(() => props.scmiId, () => {
  loadScmiById();
});
</script>
<style scoped>

</style>